package com.lovo.project.servlet.employ;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.EmployeeDO;
import com.lovo.project.service.EmployService;
import com.lovo.project.service.impl.EmployServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

    @WebServlet(name = "FindAllEmployOrFindByEmployInfoServlet",value = "/employ/findAllEmployOrFindByEmployInfo")
public class FindAllEmployOrFindByEmployInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       try {
           String type = request.getParameter("type");
           String value = request.getParameter("value");
           if (type != null || value != null) {
               value = "%" + value + "%";
           }
           String page=request.getParameter("page");
           String size=request.getParameter("size");
           System.out.println(type+value);
           //考虑到没有page和size的情况，所以使用三目运算符
           PageHelper.startPage(page==null?1:Integer.parseInt(page),size==null?3:Integer.parseInt(size));
           EmployService service = new EmployServiceImpl();
           List<EmployeeDO> list = service.selectAll(type, value);
           PageInfo pageInfo=PageInfo.of(list);
           response.getWriter().print(ResultVO.success().add(pageInfo).toJson());
       }catch (OurException o){
            response.getWriter().print(ResultVO.fail(o.getCodeEnum()));
       }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
